package cl.rizepe.playntest.core.views;

import static playn.core.PlayN.graphics;

import java.util.ArrayList;

import cl.rizepe.playntest.core.effects.EffectFactory;

import playn.core.Image;
import playn.core.gl.GLContext;
import playn.core.gl.ImageLayerGL;

public class SpriteGL extends EffectedImageLayerGL implements Sprite {

	int currentFrame;
	int frames;
	int widthFrames, heightFrames;
	ArrayList<Image> frameList;
	int width,height;
	float frecuency=30;
	float lastCall=0;
	boolean invalid=true;
	Image originalImage;
	
	public SpriteGL(ImageLayerGL imageLayer) {
		super(imageLayer);
	}

	public SpriteGL(GLContext ctx) {
		super(ctx);
	}

	public SpriteGL(GLContext ctx, Image img) {
		super(ctx, img);
	}	

	public SpriteGL(Image image, int widthFrames, int heightFrames) {
		super(graphics().createImageLayer(image.subImage(0, 0, image.width()/widthFrames, image.height()/heightFrames)));
		this.widthFrames=widthFrames;
		this.heightFrames=heightFrames;
		this.originalImage=image;
		initFrameList(image, widthFrames, heightFrames);
		//imageLayer.setWidth(width);
		//imageLayer.setHeight(height);
		currentFrame=0;
	//	Image frame=image.subImage(0, 0, 50, 50);
		//imageLayer = graphics().createImageLayer(frameList.get(currentFrame));
		setImage(frameList.get(currentFrame));
		
	}
	
	public int getWidthFrames() {
		return widthFrames;
	}

	public void setWidthFrames(int widthFrames) {
		this.widthFrames = widthFrames;
	}

	public int getHeightFrames() {
		return heightFrames;
	}

	public void setHeightFrames(int heightFrames) {
		this.heightFrames = heightFrames;
	}
	
	

	protected void initFrameList(Image image, int widthFrames, int heightFrames) {
		frames=widthFrames*heightFrames;
		frameList=new ArrayList<Image>();
		width=(int) (image.width()/widthFrames);
		height = (int) (image.height()/heightFrames);
		for (int i=0;i<frames;i++){
			int m=i%widthFrames;
			int n=i/widthFrames;
			frameList.add(image.subImage(width*m, height*n, width, height));
		}
	}
	
	public void update(float delta){
		delta+=lastCall;
		if (delta>=1/frecuency)
		{
			setFrame(currentFrame+(int)(delta*frecuency));
			currentFrame%=frames;
			invalid=true;
			lastCall=(int)(delta%frecuency);
			}
		else
			lastCall=delta;
		//imageLayer.setImage(frameList.get((currentFrame/frecuency)%frames));
		updateEffects();
	}
		
	public void advanceFrame(){
		setFrame(currentFrame+1);
		invalid=true;
		refresh();
	}
	
	public void setFrame(int i){
		currentFrame=i%frames;
		invalid=true;
		refresh();
	}
		
	public Image getFrame(int i){
		return frameList.get(i%frames);
	}
	
	public void refresh(){
		if (invalid){
			setImage(frameList.get(currentFrame));
			invalid=false;
		}
	}
	
	public Image getImage(){
		return frameList.get(currentFrame);
	}
	
	@Override
	public SpriteGL clone(){
		SpriteGL sprite= new SpriteGL(originalImage,widthFrames,heightFrames);
		//LayerState ls = new LayerState(graphics().createImageLayer(imageLayer.image()));
		EffectFactory.duplicateImageLayerAttributes(this,sprite);
		sprite.setFrecuency(this.frecuency);
		return sprite;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public float getFrecuency() {
		return frecuency;
	}

	public void setFrecuency(float frecuency) {
		this.frecuency = frecuency;
	}

}
